package com.baozun.houji.order.adapter

import android.text.style.ClickableSpan
import android.view.View
import com.baozhun.mall.common.base.BaseListAdapter
import com.baozhun.mall.common.model.bean.LogisticsInfoBean
import com.baozhun.mall.common.util.StringUtils
import com.baozun.houji.order.BR
import com.baozun.houji.order.R
import com.baozun.houji.order.databinding.ItemLogisticDetailBinding
import com.blankj.utilcode.util.IntentUtils
import com.blankj.utilcode.util.SpanUtils
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder

/**
 * @Title：物流信息
 * @Description：
 * @Author：xing.luo
 * @Since：2022/11/20
 * @Version：1.0.0
 */
class LogisticInfoAdapter(data: MutableList<LogisticsInfoBean>?) :
    BaseListAdapter<LogisticsInfoBean, ItemLogisticDetailBinding>(
        R.layout.item_logistic_detail, data
    ) {

    override fun convert(holder: BaseDataBindingHolder<ItemLogisticDetailBinding>, item: LogisticsInfoBean) {
        super.convert(holder, item)
        holder.dataBinding?.setVariable(BR.model, item)
        holder.dataBinding?.setVariable(BR.isFirstPosition, holder.layoutPosition == 0)
        holder.dataBinding?.setVariable(BR.isLastPosition, holder.layoutPosition == data.size - 1)
        var phoneNumber = StringUtils.getPhoneNumber(item.state_desc.toString())
        if (phoneNumber?.isNotEmpty() == true) {
            var stateDescArray = item.state_desc?.split(phoneNumber)
            SpanUtils.with(holder.dataBinding?.tvLogisticsStatusDesc)
                .append(stateDescArray?.get(0).toString())
                .append(phoneNumber).setForegroundColor(holder.itemView.context.getColor(com.baozhun.mall.res.R.color.color_3052AD)).setClickSpan(object : ClickableSpan() {
                    override fun onClick(p0: View) {
                        holder.itemView.context.startActivity(IntentUtils.getDialIntent(phoneNumber))
                    }
                })
                .append(if (stateDescArray?.size ?: 1 > 1) stateDescArray?.get(1).toString() else "").create()
        } else {
            holder.dataBinding?.tvLogisticsStatusDesc?.text = item.state_desc
        }
    }

}